package dao;

import domain.Employee;
import utils.DateAndString;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @version 1.0
 * @autho 3505
 * @date 2018/6/5
 */
public class EmployeeDaoFile implements BasicDao  {
    private List<Employee> list=new ArrayList<>();

    static {
        File file = new File("D:\\text.txt");
        //判断文件夹是否存在,如果不存在则创建文件夹
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    //增加一个对象
    public int add(Employee employee){
        list.add(employee);
        try {
            writeFile(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;


    }

    @Override
    //输入姓名修改工资
    public int modify(Employee employee) {
        Iterator<Employee> it=list.iterator();
        int flag=0;
        while (it.hasNext()){
            Employee employee1=it.next();
            if (employee1.getEmployeeName().equals(employee.getEmployeeName())){
                employee1.setSalary(employee.getSalary());
                flag=1;
                break;
            }
        }
        if(flag==0){
            System.out.println("查无此人");
        }else {
            System.out.println("修改成功");
            try {
                writeFile(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        }
        return 0;
    }

    @Override
    //输入姓名，删除叫这个名字的人
    public int delete(String name) {

        Iterator<Employee> it=list.iterator();
        int flag=0;
        while (it.hasNext()){
            Employee employee=it.next();
            if (employee.getEmployeeName().equals(name)){
                list.remove(employee);
                flag=1;
                break;
            }
        }
        if(flag==0){
            System.out.println("查无此人");
            return 0;
        }else {
            System.out.println("删除成功");
            try {
                writeFile(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        }


    }

    @Override
    //输入姓名，输出这个人的信息
    public Employee searchOne(String name) {
        try {
            list=readFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(list.size()!=0){
            Iterator<Employee> it=list.iterator();
            Employee e1=new Employee();
            while (it.hasNext()){
                Employee employee=it.next();
                if (employee.getEmployeeName().equals(name)){
                    System.out.println(employee);

                    break;
                }
            }
            return e1;

        }
        System.out.println("查无此人");
        return null;
    }

    @Override
    public List<Employee> searchAll() {
        try {
            list=readFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Employee> list1=new ArrayList<>();
        Iterator<Employee> it=list.iterator();
        int flag=0;
        while (it.hasNext()){
            Employee employee=it.next();
            //System.out.println(employee);
            list1.add(employee);
        }
        return list1;
    }
    public List<Employee> readFile()throws Exception{
        List<Employee> list2=new ArrayList();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream("D:\\text.txt"),"utf-8"));
        String str;
        String s[];
        DateAndString date=new DateAndString();
        while ((str=bufferedReader.readLine())!=null){
            s=str.split(" ");
            if(s.length==7){
                Employee employee=new Employee(Integer.valueOf(s[0]),s[1],s[2],date.stringToDate(s[3]),date.stringToDate(s[4])
                        ,Float.valueOf(s[5]),Integer.valueOf(s[6]));
                list2.add(employee);
            }

        }
        if (bufferedReader!=null){
            bufferedReader.close();
        }
        return  list2;

    }
    public void writeFile(List<Employee> list2)throws Exception{
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\text.txt"),"utf-8"));
        Iterator<Employee> it=list2.iterator();
        Employee employee=null;
        String str;
        while (it.hasNext()){
            employee=it.next();
            str=employee.getEmployeeID()+" "+employee.getEmployeeName()+" "+employee.getSex()+" "+new SimpleDateFormat("yyyy-MM-dd").format(employee.getDate())+" "
                    +new SimpleDateFormat("yyyy-MM-dd").format(employee.getHireDate())+" "+employee.getSalary()+" "+employee.getEmployeeID();
            bufferedWriter.write(str);
            bufferedWriter.newLine();
        }

        if (bufferedWriter!=null){
            bufferedWriter.close();
        }
    }
}
